package com.dq.Week1.day05.homeWork;

import java.util.Random;
import java.util.Scanner;

public class Demo {
    public static void main(String[] args) {
        UserUtils userUtils = new UserUtils();
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("请选择你要的功能:1.注册\t2.登录\t3.退出");
            String s = scanner.next();
            switch (s) {
                case "1":

                    System.out.println("请输入用户名");
                    String username1 = scanner.next();
                    System.out.println("请输入密码");
                    String password1 = scanner.next();
                    if(userUtils.register(username1,password1))
                    {
                        System.out.println("注册成功");
                    }else {
                        System.out.println("注册失败");
                    }

                    break;
                case "2":
                    System.out.println("请输入用户名：");
                    String username2 = scanner.next();
                    System.out.println("请输入密码：");
                    String password2 = scanner.next();
                    String systemCode = code();
                    System.out.println("请输入验证码："+systemCode);
                    String userCode = scanner.next();
                    if (systemCode.equals(userCode)) //验证码输入成功
                    {
                        if(userUtils.login(username2,password2))
                        {
                            System.out.println("登陆成功");
                            return;
                        }else{
                            System.out.println("登陆失败");
                        }
                    }else {
                        System.out.println("验证码输入错误！！！");
                    }

                    break;
                case "3":
                    System.out.println("欢迎下次使用");
                    return;


            }
        }
    }

    public static String code() {
        String[] s = {"1", "2", "3", "4", "5", "6", "7", "8"};
        Random random = new Random();
        String code = "";
        for (int i = 0; i < 4; i++) {
            int index = random.nextInt(s.length);//[0,8)
            code = code + s[index];
        }
        return code;
    }

}
